package com.lvwenguo.controller.input;

import cn.itcast.commons.CommonUtils;
import com.lvwenguo.model.InputProductPurchase;
import com.lvwenguo.service.PurchaseServiceInter;
import com.lvwenguo.service.serviceImple.PurchaseServiceImple;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

//商品录入表单数据
@WebServlet(urlPatterns = "/ProductInputServlet")
public class ProductInputServlet extends HttpServlet {
    PurchaseServiceInter purchaseServiceImpol =new PurchaseServiceImple();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {



            try {
                InputProductPurchase inputProductPurchase = CommonUtils.toBean(req.getParameterMap(), InputProductPurchase.class);
                System.out.println(inputProductPurchase);
           /* 1、保质日期>进货日期>生产日期；防止进货时间不准确
                        * 3、销售单价要大于进货单价
                        *
          */
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date exp_date = inputProductPurchase.getExp_date();
                Date purchase_date = inputProductPurchase.getPurchase_date();
                Date pro_date = inputProductPurchase.getPro_date();
                //判断保质日期>进货日期>生产日期
                if(exp_date.after(purchase_date) && purchase_date.after(pro_date)){
                    System.out.println("日期正确");
                    //日期正确在判断价格
                    if(inputProductPurchase.getSale_price()>inputProductPurchase.getPurchase_price()){
                        System.out.println("商品价格正确");
                        //全部正确后即可进行以下操作
                        //取出当前登录用户session
                        String sessionname = (String)req.getSession().getAttribute("sessionname");
                        //调用商品录入方法，实现商品信息录入
                        String error = purchaseServiceImpol.InputProductData(inputProductPurchase, sessionname);
                        //表示有错误信息
                        if(error.equals("成功修改")){

                            req.setAttribute("inputProductPurchase",inputProductPurchase);
                            req.getRequestDispatcher("InputProduct/purchase/purchase_input").forward(req,resp);
                        }
                        else{
                            req.setAttribute("inputProductPurchase",inputProductPurchase);
                            req.setAttribute("error",error);
                            req.getRequestDispatcher("InputProduct/purchase/purchase_input").forward(req,resp);
                        }
                    } else{
                        System.out.println("价格不正确");
                        req.setAttribute("inputProductPurchase",inputProductPurchase);
                        req.setAttribute("error","价格不正确");
                        req.getRequestDispatcher("InputProduct/purchase/purchase_input").forward(req,resp);

                    }
                }
                else{
                    System.out.println("日期顺序不正确");
                    req.setAttribute("inputProductPurchase",inputProductPurchase);
                    req.setAttribute("error","日期顺序不正确");
                    req.getRequestDispatcher("InputProduct/purchase/purchase_input").forward(req,resp);

                }

            }catch (Exception e){
                try {
                    req.setAttribute("error","检查信息是否正确填入");
                    req.getRequestDispatcher("InputProduct/purchase/purchase_input").forward(req,resp);
                } catch (ServletException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }





    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
